// random/pages/center/center.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        rule_alert: 0,
        notice_alert: 0,
        alert1: 0,
        share_detail: "",
        alert1: 0,
        isReward: 1,
        showClass: false,
        curIndex: -1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        console.log(options);
        this.setData({
            id: options.id || "",
            activity_id: options.activity_id || "",
            main_member_id: options.main_member_id || "",
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getMyInfo();
        if (this.data.activity_id) {
            this.openShareRecord();
        }
        this.getSet();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        return {
            title: "你有一个盲盒待领取！",
            path: `/random/pages/center/center?activity_id=${this.data.activity_id}&main_member_id=${this.data.info.m_id}`
        };
    },
    shareRecord: function() {
        var that = this;
        common.request({
            endpoint: "Random/manage/share-record",
            method: "post",
            data: {
                activity_id: that.data.activity_id
            },
            success: res => {}
        });
    },
    openShareRecord: function() {
        var that = this;
        common.request({
            endpoint: "Random/manage/open-share-record",
            method: "post",
            data: {
                activity_id: that.data.activity_id,
                main_member_id: that.data.main_member_id
            },
            success: res => {}
        });
    },
    getMyInfo: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                console.log(res);
                that.setData({
                    info: res.extra
                });
            }
        });
    },
    getSet: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Random/manage/activity",
            method: "post",
            data: {
                bill_id: that.data.id,
                activity_id: that.data.activity_id
            },
            success: res => {
                var products1 = res.extra.products;
                var reward = {};
                products1.map(x => {
                    // var animationMain = wx.createAnimation({
                    //   duration:400,
                    //   timingFunction:'linear'
                    //  })
                    // x.animationMain=animationMain;
                    x.chooseGid = 1;
                    if (x.chose == 1) {
                        reward = x;
                    }
                });
                console.log(reward);
                that.setData({
                    delivery_type: res.extra.delivery_type,
                    activity_id: res.extra.activity_id,
                    products1: products1,
                    set: res.extra.set,
                    reward: reward
                });
                if (res.message != "success") {
                    wx.showModal({
                        title: "提示！",
                        content: res.message,
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                wx.reLaunch({
                                    url: `/pages/index/index`
                                });
                            }
                        }
                    });
                }
            },
            fail(res) {
                wx.showToast({
                    icon: "none",
                    title: res.message,
                    duration: 2e3,
                    success: function() {
                        setTimeout(() => {
                            wx.reLaunch({
                                url: `/pages/index/index`
                            });
                        }, 2e3);
                    }
                });
            }
        });
    },
    rotateFn: function(e) {
        let _this = this;
        // this.setData({
        //   curIndex:e.currentTarget.dataset.idx
        // })
                if (!_this.data.isReward) {
            wx.showToast({
                icon: "none",
                title: "不可重复抽奖"
            });
            return false;
        }
        var products1 = _this.data.products1;
        var reward = _this.data.reward;
        var bill_id = _this.data.id;
        products1[e.currentTarget.dataset.idx].chooseGid = 0;
        _this.setData({
            products1: products1
        });
        if (reward && reward.product_item_id) {
            setTimeout(() => {
                wx.showModal({
                    title: "提示",
                    content: "恭喜你获得奖品" + reward.name,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            if (_this.data.delivery_type == 0) {
                                _this.remind(reward.product_item_id, reward.id, bill_id);
                            } else {
                                wx.navigateTo({
                                    url: `/random/pages/place_order/place_order?product_item_id=${reward.product_item_id}&&id=${reward.id}&&bill_id=${bill_id}`
                                });
                            }
                        }
                    }
                });
            }, 1e3);
        } else {
            common.request({
                endpoint: "/Random/manage/bill-off",
                method: "post",
                data: {
                    bill_id: bill_id
                },
                success: res => {
                    wx.showModal({
                        title: "提示！",
                        content: "很遗憾,您未获得奖品！",
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                wx.reLaunch({
                                    url: `/pages/index/index`
                                });
                            }
                        }
                    });
                }
            });
        }
        //   var key=products1[e.currentTarget.dataset.idx];
        //   // this.animation_main = wx.createAnimation({
        //   //   duration:400,
        //   //   timingFunction:'linear'
        //   //  })
        //   products1[e.currentTarget.dataset.idx].animationMain.rotateY(180).step();
        //   products1[e.currentTarget.dataset.idx].animationMain=products1[e.currentTarget.dataset.idx].animationMain.export();
        //    this.setData({
        //     products1: products1
        //    })
        },
    remind: function(product_item_id, product_id, bill_id) {
        let that = this;
        common.request({
            endpoint: "/Random/manage/place-order",
            method: "post",
            data: {
                product_id: product_id,
                product_item_id: product_item_id,
                bill_id: bill_id
            },
            header: {
                "content-type": "application/json"
            },
            success(res) {
                wx.showToast({
                    icon: "none",
                    title: "领取成功,等待奖品发放",
                    duration: 3e3
                });
                var if_share = res.extra.if_share;
                var share_detail = res.extra.share_detail;
                if (if_share == 1) {
                    that.setData({
                        isReward: 0,
                        alert1: 1,
                        share_detail: share_detail,
                        activity_id: res.extra.activity_id
                    });
                } else {
                    setTimeout(() => {
                        wx.reLaunch({
                            url: "/random/pages/order/order"
                        });
                    }, 3e3);
                }
                //  setTimeout(()=>{ 
                //    wx.reLaunch({
                //      url: '/pages/index/index',
                //    })
                //  },3000)
                        },
            fail(res) {
                wx.showToast({
                    icon: "none",
                    title: res.message,
                    duration: 3e4
                });
            }
        });
    },
    alert_close2: function(e) {
        this.setData({
            alert1: 0
        });
    },
    show_rule: function() {
        this.setData({
            rule_alert: 1
        });
    },
    close_rule: function() {
        this.setData({
            rule_alert: 0
        });
    },
    show_notice: function() {
        this.setData({
            notice_alert: 1
        });
    },
    close_notice: function() {
        this.setData({
            notice_alert: 0
        });
    },
    sharerecord: function() {
        this.shareRecord();
    }
});